<?php
class Upload {
	public function up() {
		//		重组数组
		$arr = $this -> resetArr();
		//		筛选
		foreach ($arr as $v) {
			if (!$this -> filter($v)) {
				success('失败了', 'index1.php');
				return false;
			}
		}
		foreach ($arr as $v) {
			success('成功了', 'index1.php');
			$this -> move($v);
		}
	}

	//移动上传
	private function move($v) {
		$path = './upload/' . date('ymd');
		is_dir($path) || mkdir($path, 0777, true);
		$type = ltrim(strrchr($v['name'], '.'), '.');
		$filename = time() . mt_rand(0, 999999) . '.' . $type;
		$dest = $path . '/' . $filename;
		move_uploaded_file($v['tmp_name'], $dest);
	}

	//筛选
	private function filter($v) {
		if (is_uploaded_file($v['tmp_name'])) {
			return true;
		}
		if ($v['error'] == 0) {
			return true;
		}
		if (is_array(ltrim(strrchr($v['name'], '.'), '.'), array('jpg', 'png', 'gif', ))) {
			return true;
		}
		if ($v['size'] == 1024 * 2 * 1024) {
			return true;
		}
		return false;
	}

	private function resetArr() {
		$arr = array();
		$v = current($_FILES);
		foreach ($v['name'] as $key => $value) {
			$arr[] = array('name' => $value, 'type' => $v['type'][$key], 'tmp_name' => $v['tmp_name'][$key], 'error' => $v['error'][$key], 'size' => $v['size'][$key]);
		}
		return $arr;
	}

}
?>